package com.tao.camera;


import android.content.Context;
import android.graphics.Point;
import android.hardware.Camera;
import android.media.AudioManager;
import android.util.Log;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.View;
import android.view.WindowManager;

import java.util.HashMap;
import java.util.Map;

/**
 * 摄像头管理类
 */
public class CameraManager implements ICameraOperate {
    private static CameraManager cameraManager;
    private String tag = "CameraManager";

    private CameraManager() {
    }

    public static CameraManager getInstance() {
        if (cameraManager == null) {
            synchronized (CameraManager.class) {
                if (cameraManager == null)
                    cameraManager = new CameraManager();
            }
        }
        return cameraManager;
    }

    Context context;

    Map<Integer, DualCameraHelper> cameraHelperMap = new HashMap<>();

    public CameraManager init(Context context) {
        this.context = context.getApplicationContext();
        int numberOfCameras = getNumberOfCameras();
        for (int i = 0; i < numberOfCameras; i++) {
            getOrCreateHelperWithId(i);
        }
        return this;
    }

    /**
     * 获取摄像头个数
     *
     * @return
     */
    private int getNumberOfCameras() {
        return Camera.getNumberOfCameras();
    }

    public void initCameraConfig(int id, Point perviewSize, View perView) {
        int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
        initCameraConfig(id, perviewSize, perView, rotation, false, null);
    }

    public void initCameraConfig(int id, Point perviewSize, View perView, CameraListener cameraListener) {
        int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
        initCameraConfig(id, perviewSize, perView, rotation, false, cameraListener);
    }


    public void initCameraConfig(int id, Point perviewSize, View perView, int rotation, boolean mirror, CameraListener cameraListener) {

        DualCameraHelper dualCameraHelper = getOrCreateHelperWithId(id);
        dualCameraHelper.release();
        dualCameraHelper.setSpecificCameraId(id);
        dualCameraHelper.setSpecificPreviewSize(perviewSize);
        dualCameraHelper.setPreviewViewSize(perviewSize);
        dualCameraHelper.setRotation(rotation);
        dualCameraHelper.setMirror(mirror);
        dualCameraHelper.setPreviewDisplayView(perView);
        dualCameraHelper.setCameraListener(cameraListener);

        if (!(perView instanceof SurfaceView || perView instanceof TextureView)) {
            Log.i(tag, "perView must be usage  and  it's SurfaceView or TextureView");
            return;
        }
        
        dualCameraHelper.init();

    }

    public void initCameraConfig(int id, DualCameraHelper.Builder builder) {
        DualCameraHelper dualCameraHelper = getOrCreateHelperWithId(id);
        dualCameraHelper.setBuilder(builder);
        if (!dualCameraHelper.isStopped() || dualCameraHelper.isPerviewIng()) {
            dualCameraHelper.stop();
        }

        dualCameraHelper.init();
    }

    @Override
    public void perviewCamera(int id) {
        getOrCreateHelperWithId(id).start();
    }

    public void takePic(int id, int type) {
        takePic(id, type, 30);
    }

    public void takePic(int id, int type, long time) {
        Log.i(tag, "takePic");
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
        DualCameraHelper dualCameraHelper = getOrCreateHelperWithId(id);
        if (type == 0) {
            try {
                time = time > 2000 ? 2000 : time;
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        dualCameraHelper.takePic(type);
    }

    public void takePic(int id) {
        takePic(id, 0);
    }

    @Override
    public void releaseCamera(int id) {
        getOrCreateHelperWithId(id).release();
    }

 


    public void switchPerviewView(int id, View perviewView ,boolean mirror) {
        if (perviewView == null || !(perviewView instanceof SurfaceView || perviewView instanceof TextureView)) {
            Log.i(tag, "perView must be usage  and  it's SurfaceView or TextureView");
            return;
        }
        DualCameraHelper dualCameraHelper = getOrCreateHelperWithId(id);
        dualCameraHelper.stop();
        dualCameraHelper.setPreviewDisplayView(perviewView);
        dualCameraHelper.setMirror(mirror);
        dualCameraHelper.init();
    }

    @Override
    public void setCameraListener(int id, CameraListener cameraListener) {
        DualCameraHelper dualCameraHelper = getOrCreateHelperWithId(id);
        dualCameraHelper.setCameraListener(cameraListener);
    }

    public DualCameraHelper getOrCreateHelperWithId(int id) {

        DualCameraHelper dualCameraHelper = cameraHelperMap.get(id);
        if (dualCameraHelper == null) {
            Log.e(tag, " id:" + id);
            dualCameraHelper = new DualCameraHelper.Builder()
                    .rotation(((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation())
                    .specificCameraId(id)
                    .isMirror(false)
                    .build();
            cameraHelperMap.put(id, dualCameraHelper);
        }
        return dualCameraHelper;
    }


    public void stopPerview(int id) {
        getOrCreateHelperWithId(id).stop();
    }

    /**
     * 添加额外的摄像头回调
     *
     * @param id
     * @param cameraListener
     */
    public void addExterCameraListener(int id, CameraListener cameraListener) {
        getOrCreateHelperWithId(id).addExterCameraListener(cameraListener);
    }

    public void reMoveExterCameraListener(int id, CameraListener cameraListener) {
        getOrCreateHelperWithId(id).reMoveExterCameraListener(cameraListener);
    }


    public void clearCameraListener(int cameraId) {
        getOrCreateHelperWithId(cameraId).clearCameraListener();
    }
    
    
}
